package com.xialing.controller;


import com.xialing.controller.request.AccountLoginRequest;
import com.xialing.controller.request.AccountRegisterRequest;
import com.xialing.enums.BizCodeEnum;
import com.xialing.service.AccountService;
import com.xialing.service.FileService;
import com.xialing.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/api/account/v1")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    public JsonData uploadUserImage(@RequestPart("file")MultipartFile file) {

        String res = fileService.uploadUserImage(file);

        return res != null ? JsonData.buildSuccess(res) : JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);
    }

    /**
     *  注册
     * @param accountRegisterRequest
     * @return
     */
    @PostMapping("/register")
    public JsonData register(@RequestBody AccountRegisterRequest accountRegisterRequest) {

        JsonData jsonData = accountService.register(accountRegisterRequest);
        return jsonData;
    }

    @PostMapping("/login")
    public JsonData login(@RequestBody AccountLoginRequest accountLoginRequest) {

        JsonData jsonData = accountService.login(accountLoginRequest);
        return jsonData;
    }

}

